package top.ztf.erp.web;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import top.ztf.erp.domain.Role;
import top.ztf.erp.service.RoleService;
import top.ztf.erp.service.imp.RoleServiceImp;
import top.ztf.erp.utils.BeanValidatorUtils;
import top.ztf.erp.utils.ResponseClient;
import top.ztf.erp.vo.PermissionVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@WebServlet("/role")
public class RoleServlet extends BaseServlet{

    public void getRolePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RoleService roleService = new RoleServiceImp();
        String pageNo = req.getParameter("pageNo");
        String pageSize = req.getParameter("pageSize");
        PageInfo<Role> rolePage = roleService.getRolePage(pageNo, pageSize);
        req.setAttribute("rolePage",rolePage);
        req.getRequestDispatcher("WEB-INF/qx/role/list.jsp").forward(req,resp);
    }

    public void getRoleDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        RoleService roleService = new RoleServiceImp();
        Role role = roleService.getRoleById(id);
        req.setAttribute("role",role);
        req.getRequestDispatcher("WEB-INF/qx/role/detail.jsp").forward(req,resp);
    }

    public void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        Map<String, String[]> parameterMap = req.getParameterMap();
        Role role = new Role();
        BeanUtils.populate(role,parameterMap);

        Map<String, String> validate = BeanValidatorUtils.validate(role, Role.Add.class);
        if (!validate.isEmpty()){
            ResponseClient.fail(JSONObject.toJSONString(validate),resp);
            return;
        }
        RoleService roleService = new RoleServiceImp();
        int count =  roleService.save(role);
        ResponseClient.common(count,resp);
    }

    public void gerPermissionByRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        String roleId = req.getParameter("id");
        RoleService roleService = new RoleServiceImp();
        List<PermissionVo> permissionVoList =  roleService.getPermissionByRole(roleId);
        resp.getWriter().print(JSONObject.toJSONString(permissionVoList));
    }

    public void getAssign(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String roleId = req.getParameter("id");
        RoleService roleService = new RoleServiceImp();
        Role roleById = roleService.getRoleById(roleId);
        req.setAttribute("role",roleById);
        req.getRequestDispatcher("/WEB-INF/qx/role/assign.jsp").forward(req,resp);
    }

    public void getAssignPermission(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String roleId = req.getParameter("id");
        RoleService roleService = new RoleServiceImp();
        List<Map<String,Object>> assign = roleService.getAssignPermission(roleId);
        resp.getWriter().print(JSONObject.toJSONString(assign));
    }

    public void updatePermissionRole(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //这里有个坑 接收集合 ,必须要用 array[]
        String[] arrays = req.getParameterValues("array[]");
        String roleId = req.getParameter("id");
        RoleService roleService = new RoleServiceImp();
        int count  = roleService.updatePermissionByRole(roleId,arrays);
        ResponseClient.common(count,resp);
    }

    public void getUpdateByRole(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String roleId = req.getParameter("id");
        RoleService roleService = new RoleServiceImp();
        Role roleById = roleService.getRoleById(roleId);
        req.setAttribute("role",roleById);
        req.getRequestDispatcher("WEB-INF/qx/role/edit.jsp").forward(req,resp);
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException, InvocationTargetException, IllegalAccessException {
        Map<String, String[]> parameterMap = req.getParameterMap();
        RoleService roleService = new RoleServiceImp();
        Role role = new Role();
        BeanUtils.populate(role,parameterMap);

        Map<String, String> validate = BeanValidatorUtils.validate(role, Role.Update.class);
        if (!validate.isEmpty()){
            ResponseClient.fail(JSONObject.toJSONString(validate),resp);
            return;
        }

        int count = roleService.update(role);
        ResponseClient.common(count,resp);
    }

    public void deleteRole(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //这里有个坑 接收集合 ,必须要用 array[]
        String[] arrays = req.getParameterValues("array[]");
        RoleService roleService = new RoleServiceImp();
        int count  = roleService.deleteRole(arrays);
        ResponseClient.common(count,resp);
    }

    public void verifyRoleName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //这里有个坑 接收集合 ,必须要用 array[]
        String name = req.getParameter("name");
        String id = req.getParameter("id");
        RoleService roleService = new RoleServiceImp();
        int count =  roleService.selectRoleByName(id,name);
       if (count==0){
           //可用
           ResponseClient.success(resp);
       }else {
           //不可用
           ResponseClient.fail(resp);
       }
    }

}
